package com.task.controller.system;

import com.task.common.annotation.Log;
import com.task.common.entity.sys.model.user.UserRegisterModel;
import com.task.common.entity.sys.model.user.UserUpdateModel;
import com.task.common.entity.sys.SysUser;
import com.task.common.entity.sys.vo.UserVO;
import com.task.common.enums.BusinessType;
import com.task.common.util.model.ModelUtil;
import com.task.common.util.body.JsonResult;
import com.task.common.util.body.ResultTool;
import com.task.common.util.string.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.task.service.IUserService;

/**
 * @author Huzz
 * @created 2021-12-06 15:50
 */
@Api(tags = "用户管理")
@RestController
@RequestMapping("/system/user")
public class UserController {

    @Autowired
    private IUserService userService;

    @Autowired
    private ModelUtil modelUtil;

    @Log(title = "操作日志", businessType = BusinessType.INSERT, isSaveResponseParam = true)
    @ApiOperation(value = "管理员用户注册")
    @PostMapping("/addUser")
    public JsonResult register(@RequestBody UserRegisterModel model) {
        if (StringUtils.isNotNull(userService.findUserByUsername(model.getUsername()))) {
            return userService.register((SysUser) modelUtil.toEntity(model, SysUser.class)) ? ResultTool.success() : ResultTool.fail();
        }
        return ResultTool.fail("该用户名已被注册");
    }

    /**
     * 操作日志中不能保存用户修改提交的参数, 以免用户信息被导出
     * @param model
     * @return
     */
    @Log(title = "操作日志", businessType = BusinessType.UPDATE, isSaveResponseParam = true)
    @ApiOperation(value = "根据id修改用户信息")
    @PutMapping("/updateUserById")
    public JsonResult updateUserById(UserUpdateModel model) {
        return userService.updateUser((SysUser) modelUtil.toEntity(model, SysUser.class)) ? ResultTool.success() : ResultTool.fail();
    }

    @Log(title = "操作日志", businessType = BusinessType.DELETE, isSaveResponseParam = true)
    @ApiOperation(value = "删除用户信息")
    @DeleteMapping("/delUserById")
    public JsonResult delUserById(Integer userId) {
        return userService.delUser(userId) ? ResultTool.success() : ResultTool.fail();
    }

    @Log(title = "操作日志", businessType = BusinessType.SELECT, isSaveRequestParam = true)
    @ApiOperation(value = "查询分页列表")
    @GetMapping("/finUserListPage")
    public JsonResult finUserListPage(@ApiParam(value = "页号") @RequestParam(defaultValue = "1", required = false) Integer pageNum,
                                      @ApiParam(value = "页数") @RequestParam(defaultValue = "10", required = false) Integer pageSize,
                                      @ApiParam(value = "userModel") UserVO model
                                      ) {
        return ResultTool.success(userService.findUserPage(pageNum, pageSize, model));
    }

    @Log(title = "操作日志", businessType = BusinessType.SELECT)
    @ApiOperation(value = "根据id进行查询")
    @GetMapping("/findUserById")
    public JsonResult findUserById(Integer userId) {
        return ResultTool.success(modelUtil.toEntity(userService.findUserById(userId), UserVO.class));
    }

    @Log(title = "操作日志", businessType = BusinessType.SELECT)
    @ApiOperation(value = "根据用户名进行查询")
    @GetMapping("/finUserByUsername")
    public JsonResult finUserByUsername(String username) {
        return ResultTool.success(modelUtil.toEntity(userService.findUserByUsername(username), UserVO.class));
    }
}
